layui.use([ 'table', 'form' ], function() {
	var table = layui.table, form = layui.form;

	table.render({
		elem : '#tb',
		height: 'full-150',
		url : Base.ctxPath + '/xs/student/list',
		cols : [ [ {
			type : 'radio'
		}, {
			field : 'name',
			title : '姓名'
		}, {
			field : 'age',
			title : '年龄'
		}, {
			field : 'ctime',
			title : '日期'
		}, {
			field : 'cname',
			title : '班级'
		} ] ],
		page : true
	});

	function search(params) {
		table.reload('tb', {
			page: {
				curr : 1
			},
			where: params || {}
		});
	}

	// 提交查询
	form.on('submit(search)', function(data) {
		search(data.field);
		return false;
	});

	// 新增
	$("#add").click(function() {
		layer.open({
      		type: 2,
      		title: '增加',
      		content: Base.ctxPath + '/xs/student/add',
      		maxmin: true,
      		area: ['500px', '350px'],
      		btn: ['确定', '取消'],
      		yes: function(index, layero){
        		var frm = layero.find('iframe').contents().find("#myform");
        		$.post(Base.ctxPath + '/xs/student/save', frm.serialize(), function(data) {
        			table.reload('tb');
        			layer.close(index);
        		}, 'JSON');
      		}
    	}); 
	});

	// 编辑
	$("#edit").click(function() {
		var data = Base.getSelected(table, 'tb');
		if (data.length == 0) {
			Base.fail("至少选择一行数据");
			return false;
		}
		
		var id = data[0].id;
		layer.open({
      		type: 2,
      		title: '修改',
      		content: Base.ctxPath + '/xs/student/get/' + id,
      		maxmin: true,
      		area: ['500px', '350px'],
      		btn: ['确定', '取消'],
      		yes: function(index, layero){
        		var frm = layero.find('iframe').contents().find("#myform");
        		$.post(Base.ctxPath + '/xs/student/save', frm.serialize(), function(data) {
        			table.reload('tb');
        			layer.close(index);
        		}, 'JSON');
      		}
    	}); 
	});

	// 删除
	$("#del").click(function() {
		var data = Base.getSelected(table, 'tb');
		if (data.length == 0) {
			Base.fail("至少选择一行数据");
			return false;
		}
		
		var id = data[0].id;
		Base.confirm("是否删除该记录？", function() {
			$.ajax({
				url : Base.ctxPath + "/xs/student/del/" + id,
				type : "post",
				success : function(result) {
					if (result.success) {
						Base.alert(result.message);
						search();
					} else {
						Base.fail(result.message);
					}
				}
			});
		});
	});

});